package org.agile4j.easypoi;

import com.alibaba.fastjson.JSON;
import org.agile4j.easypoi.entity.*;
import org.agile4j.easypoi.utils.FreeMarkerUtil;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.dom4j.Document;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;
import org.jeecgframework.poi.excel.ExcelImportUtil;
import org.jeecgframework.poi.excel.entity.ImportParams;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.util.*;

/**
 * Created by Administrator on 2016/7/21.
 */
public class CatalogHelper {
    public void exportCatalog(int createFlag) throws Exception{
        ImportParams params = new ImportParams();
        params.setHeadRows(1);
        params.setSheetNum(1);
        File file = new File("E:\\IDEA15\\gitosc\\excel-demo\\src\\main\\resources\\devtool\\菜单.xls");
        List<MenuEntity> list = ExcelImportUtil.importExcel(file, MenuEntity.class, params);

        List<CatalogEntity> catalogEntityList = new ArrayList<>() ;

        Map<String, Set<Item1Entity>> map1 = getMenu1(list) ;

        for (Map.Entry<String, Set<Item1Entity>> menu1entry : map1.entrySet()) {
            for (Item1Entity item1 : menu1entry.getValue()) {   // 循环一级目录
                // 1. 一个模块组装成一个 CatalogEntity 对象
                CatalogEntity vo = new CatalogEntity() ;
                vo.setId(item1.getId());
                vo.setDesc(item1.getDesc());
                // 1. 一级目录下二级目录集合 List<Item2Entity>
                Set<Item2Entity> item2EntitySet = getMenu2FromMenu1(list).get(item1.getId()) ; // 给1级目录绑定2级目录集合
                if (CollectionUtils.isNotEmpty(item2EntitySet)) {
                    List<Item2Entity> item2EntityList = new ArrayList<>(item2EntitySet) ;
                    for (Item2Entity item2Entity : item2EntityList) {
                        // 2.1 定义二级目录下交易集合
                        Set<TraceEntity> trace2Set = getTrace2FromMenu2(list).get(item2Entity.getId()) ;
                        if (CollectionUtils.isNotEmpty(trace2Set)) {
                            item2Entity.setTraceList(new ArrayList<TraceEntity>(trace2Set));
                        }

                        // 2.2 定义二级目录下三级目录集合
                        Set<Item3Entity> item3EntitySet = getMenu3FromMenu2(list).get(item2Entity.getId()) ;
                        if (CollectionUtils.isNotEmpty(item3EntitySet)) {
                            List<Item3Entity> item3EntityList = new ArrayList<>(item3EntitySet) ;
                            for ( Item3Entity item3Entity : item3EntityList) {
                                // 3 定义三级目录下交易集合
                                List<TraceEntity> trace3List = new ArrayList<>(getTrace3FromMenu3(list).get(item3Entity.getId())) ;
                                item3Entity.setTraceList(trace3List);
                            }
                            item2Entity.setItem3List(item3EntityList);
                        }
                    }
                    vo.setItems2List(item2EntityList);
                    catalogEntityList.add(vo);
                }
            }
        }
//        JSON.toJSONString(catalogEntityList) ;
        System.out.println(JSON.toJSONString(catalogEntityList) );

        Map<String, List<CatalogEntity>> dataMap = new HashMap<>() ;
        dataMap.put("catalogList", catalogEntityList) ;
        String result = FreeMarkerUtil.buildContent("catalog.ftl", dataMap) ;
        if (createFlag == 1) {    // 只有 createFlag 时 才生成文件
            createXml(result, new File("E:\\IDEA15\\gitosc\\excel-demo\\src\\main\\resources\\freemarker\\xml\\catalog.xml"));    // 生成文件
        }
    }

    private void createXml(String result, File file) throws Exception{
        SAXReader sax = new SAXReader() ;
        InputStream is = new ByteArrayInputStream(result.getBytes()) ;
        Document document = sax.read(is) ;
        OutputFormat format = OutputFormat.createPrettyPrint() ;
        format.setEncoding("UTF-8");
        XMLWriter out = new XMLWriter(new FileOutputStream(file), format) ;
        out.write(document);
    }

    public Map<String, Set<Item1Entity>> getMenu1(List<MenuEntity> list) {
        Map<String,Set<Item1Entity>> parentMap =new HashMap<>();// 要组装的Map
        for (MenuEntity menuEntity : list ) {
            String groupId = menuEntity.getId1() ;
            if (StringUtils.isNotEmpty(groupId)){
                Set<Item1Entity> subSet = parentMap.get(groupId) ;
                if (subSet == null ) {
                    subSet = new HashSet<>() ;
                    parentMap.put(groupId,subSet) ;
                }
                if (StringUtils.isNotEmpty(menuEntity.getId1())){
                    Item1Entity item1Entity = new Item1Entity();
                    item1Entity.setId(menuEntity.getId1());
                    item1Entity.setDesc(menuEntity.getDesc1());
                    subSet.add(item1Entity);
                }
            }
        }
        return parentMap ;
    }

    // 取得 1级目录下 2级目录集合
    public Map<String, Set<Item2Entity>> getMenu2FromMenu1(List<MenuEntity> list) {
        Map<String,Set<Item2Entity>> parentMap =new HashMap<>();// 要组装的Map
        for (MenuEntity menuEntity : list ) {
            String groupId = menuEntity.getId1() ;// 分组字段
            if (StringUtils.isNotEmpty(groupId)){
                Set<Item2Entity> subList = parentMap.get(groupId) ;
                if (subList == null) { // 第一次遍历
                    subList = new HashSet<>();
                    parentMap.put(groupId, subList) ;
                }
                if (StringUtils.isNotEmpty(menuEntity.getId2())){
                    Item2Entity item2Entity = new Item2Entity();
                    item2Entity.setId(menuEntity.getId2());
                    item2Entity.setDesc(menuEntity.getDesc2());
                    subList.add(item2Entity);
                }
            }
        }
        return parentMap ;
    }

    // 取得 2级目录下2级交易集合
    public Map<String, Set<TraceEntity>> getTrace2FromMenu2(List<MenuEntity> list) {
        Map<String,Set<TraceEntity>> parentMap =new HashMap<>();// 要组装的Map
        for (MenuEntity menuEntity : list ) {
            String groupId = menuEntity.getId2() ; // 设置分组ID
            if (StringUtils.isNotEmpty(groupId)) {
                Set<TraceEntity> subList = parentMap.get(groupId) ;
                if (subList == null){ // 第一次遍历
                    subList = new HashSet<>() ;
                    parentMap.put(groupId,subList) ;
                }
                if (StringUtils.isNotEmpty(menuEntity.getTrace2())) {
                    TraceEntity traceEntity = new TraceEntity();
                    traceEntity.setId(menuEntity.getTrace2());
                    traceEntity.setDesc(menuEntity.getTrace2Desc());
                    subList.add(traceEntity);
                }
            }
        }
        return parentMap ;
    }

    // 取得 2级目录下3级目录集合
    public Map<String, Set<Item3Entity>> getMenu3FromMenu2(List<MenuEntity> list) {
        Map<String,Set<Item3Entity>> parentMap =new HashMap<>();// 要组装的Map
        for (MenuEntity menuEntity : list ) {
            String groupId = menuEntity.getId2() ;
            if (StringUtils.isNoneEmpty(groupId)) {
                Set<Item3Entity> subList = parentMap.get(groupId) ;
                if (subList == null){
                    subList = new HashSet<>() ;
                    parentMap.put(groupId,subList);
                }
                if (StringUtils.isNotEmpty(menuEntity.getId3())) {
                    Item3Entity item3Entity = new Item3Entity() ;
                    item3Entity.setId(menuEntity.getId3());
                    item3Entity.setDesc(menuEntity.getDesc3());
                    subList.add(item3Entity);
                }
            }
        }
        return parentMap ;
    }
    // 取得 3级目录下3级交易集合
    public Map<String, Set<TraceEntity>> getTrace3FromMenu3(List<MenuEntity> list) {
        Map<String,Set<TraceEntity>> parentMap =new HashMap<>();// 要组装的Map
        for (MenuEntity menuEntity : list) {
            String groupId = menuEntity.getId3() ;
            if (StringUtils.isNotEmpty(groupId)) {
                Set<TraceEntity> subList = parentMap.get(groupId) ;
                if (subList == null) {
                    subList = new HashSet<>()  ;
                    parentMap.put(groupId,subList);
                }
                if (StringUtils.isNotEmpty(menuEntity.getTrace3())){
                    TraceEntity traceEntity = new TraceEntity();
                    traceEntity.setId(menuEntity.getTrace3());
                    traceEntity.setDesc(menuEntity.getTrace3Desc());
                    subList.add(traceEntity);
                }
            }
        }
        return parentMap;
    }
}
